#include<pthread.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<iostream>
#include<string>

namespace MY_TCP{
  // template<class T>
    class TCP_SOCK{
      private:
      TCP_SOCK()
      {}
      public:
      static int Sock()
      {
        int sock = socket(AF_INET,SOCK_STREAM,0);
        if(sock < 0)
        {
          std::cerr << "sock is error" << std::endl;
          exit(2);
        }
        else{
          return sock;
        }
      }
      static void Bind(int sock,int port)
      {
      struct sockaddr_in server;
      server.sin_addr.s_addr = INADDR_ANY;
      server.sin_port = htons(port);
      server.sin_family = AF_INET;
      socklen_t len = sizeof(server);
        if(bind(sock,(struct sockaddr*)&server,len) < 0)
        {
          std::cerr << "bind is error" << std::endl; 
          exit(3);
        }
      }
      static int Listen(int sock,int n = 5)
      {
        return listen(sock,n);
      }
      static int Accept(int sock)
      {
         struct sockaddr_in client;
         socklen_t len;
         int waiter_fd = accept(sock,(struct sockaddr*)&client,&len);
         return waiter_fd;
      }
      static void Connect(int sock,std::string ip,int port)
      {
         struct sockaddr_in client;
         client.sin_addr.s_addr = inet_addr(ip.c_str());
         client.sin_port = htons(port);
         client.sin_family = AF_INET;

         socklen_t len = sizeof(client);
        if(connect(sock,(struct sockaddr*)&client,len) == 0)
        {
          std::cout << "connect success" << std::endl;
        }
        else
        {
          std::cerr << "connect is error" << std::endl; 
          exit(5);
        }
      }
    };
}
